package cc.tuhaolicai.mt.user.mobile.tool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.util.Comparator;
import java.util.Map;

/**
 * list自定义排序
 * 该自定义排序支持多字段排序
 * 对应PageInfo的sortMap
 * 要求：排序对象属性必须是封装类型
 * @author chenli
 * @create 2017-07-20
 */
public class ToolComparatorList<T> implements Comparator<T> {
    public static final Logger log = LoggerFactory.getLogger(ToolComparatorList.class);

    /**
     * 排序条件
     */
    public Map<String, Integer> sort;

    public ToolComparatorList(){
        super();
    }

    public ToolComparatorList(Map<String, Integer> sort){
        super();
        this.sort = sort;
    }

    @Override
    public int compare(T t, T tCompare) {
        Class<?> clazz = t.getClass();
        Class<?> clazzCompare = tCompare.getClass();
        if (clazz != clazzCompare) {
            throw new RuntimeException("排序出错");
        }
        if (sort == null) {
            return 1;
        }

        for (Map.Entry<String, Integer> entry : sort.entrySet()) {
            String key = entry.getKey();
            Integer value = entry.getValue();
            try {
                Field field = clazz.getDeclaredField(key);//获取该属性
                field.setAccessible(true);//可编辑
                Object fieldValue = field.get(t);//list中对象该属性值
                field.setAccessible(false);

                Field fieldCompare = clazz.getDeclaredField(key);//获取该属性
                fieldCompare.setAccessible(true);//可编辑
                Object fieldCompareValue = fieldCompare.get(tCompare);//list中对象该属性值
                fieldCompare.setAccessible(false);
                if (fieldValue == null || fieldCompareValue == null) {
                    return 1;
//                    return (int)(fieldCompareValue.getTime() - fieldValue.getTime());
                }
                int result;
                if (ObjectType.isInteger(fieldValue)) {
                    result = (int)fieldCompareValue - (int)fieldValue;
                }else if (ObjectType.isDouble(fieldValue)) {
                    result = (int)((Double)fieldCompareValue - (Double)fieldValue);
                }else if (ObjectType.isLong(fieldValue)) {
                    result = (int)((long)fieldCompareValue - (long)fieldValue);
                }else if (ObjectType.isBoolean(fieldValue)) {
                    result = (int)fieldCompareValue - (int)fieldValue;
                }else {
                    continue;
                }
                if(result == 0){
                    continue;
                }
                if(value == 1){
                    return result;
                }
                return -result;
            } catch (NoSuchFieldException | IllegalAccessException e) {
                e.printStackTrace();
                log.warn("排序失败:" + clazz.getName());
            }

        }
        return 1;
    }

    public Map<String, Integer> getSort() {
        return sort;
    }

    public ToolComparatorList setSort(Map<String, Integer> sort) {
        this.sort = sort;
        return this;
    }
}